/*
 * 上传文件工具
 * $("#imgPicker").Uploader({server:"{{ url('admin/upload/upload') }}"});
**/
$.fn.Uploader = function(options,callback){
	var $_id = $(this).attr('id');
	var defaultoptions = {
	  	 server:null, //上传处理的php文件路径
	  	 swf:'/Public/plugins/webuploader/js/uploader.swf',
         size:2*1024*1024, //允许的文件大小
         auto:true,
         resize:false,//不压缩image
         chunked:true,//是否要分片处理大文件上传
         chunkRetry:1, //如果某个分片由于网络问题出错，允许自动重传次数
         runtimeOrder:'html5,flash',
         filelist:null, //是否有文件列表待上传
         uploadProgress:false, //是否显示上传进度条
         fileinput:'#image',
         accept:{title:'Images',extensions:'gif,jpg,jpeg,bmp,png',mimeTypes:'image/*'}
	}
	var option = $.extend({},defaultoptions,options);
    var uploader = WebUploader.create({
        resize: option.resize, 
        swf: option.swf, 
        server:option.server, 
        pick: '#'+$_id, 
        chunked: option.chunked, 
        chunkSize:option.size, 
        auto: option.auto, 
        chunkRetry:option.chunkRetry, 
        runtimeOrder: option.runtimeOrder,
        accept: option.accept,
    });
    // 当有文件被添加进队列的时候
    if(option.filelist != null && option.filelist != undefined){
        uploader.on( 'fileQueued', function( file ) {
            $list.append( '<div id="' + file.id + '" class="item">' +
                // '<h4 class="info">' + file.name + '</h4>' +
                '<p class="state">' + file.name + '  等待上传...</p>' +
                '</div>' );
        });   
    }
    // 文件上传过程中创建进度条实时显示。
    if(option.uploadProgress){
        uploader.on( 'uploadProgress', function( file, percentage ) {
            var $li = $( '#'+file.id ),
                $percent = $li.find('.progress .progress-bar');

            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<div class="progress progress-striped active">' +
                    '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                    '</div>' +
                    '</div>').appendTo( $li ).find('.progress-bar');
            }

            $li.find('p.state').text('上传中,请稍等...');
            $('#apk').val();
            // var percentage = percentage * 100 + '%';

            $percent.css( 'width', percentage * 100 + '%' );
            $('#processbar').css( 'width', percentage * 100 + '%' );
            $('#processbar').next('.t').text( parseInt(percentage * 100) + '%' );
        });
    }
    // 文件上传成功
    uploader.on( 'uploadSuccess', function( file  ,response) {
        if(response.status == 0){
            alert(response.info);
            return false;
        }
        var oldName = response.oldName;
        var filePaht = response.filePaht;
        var fileSuffixes = response.fileSuffixes;
        if(option.fileinput != null){
            $(option.fileinput).val(filePaht);
        }
        $( '#'+file.id ).find('p.state').text('已上传');
        if(typeof(callback) == 'function')
            callback(response);
    });
    // 文件上传失败，显示上传出错。
    uploader.on('uploadError', function (file) {
        var $li = $('#' + file.id),
                $error = $li.find('div.error');
        // 避免重复创建
        if (!$error.length) {
            $error = $('<div class="error"></div>').appendTo($li);
        }
        $error.text('上传失败');
    });
}